from selenium import webdriver
from selenium.webdriver.common.by import By
from cfg.cfg_permission import *
from hyrobot.common import GSTORE
import os
from hyrobot.common import STEP, CHECK_POINT, INFO
import pyautogui
import pytest
import time
import json
from selenium.common.exceptions import NoSuchElementException
import random
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import pymysql
import random
# 打开数据库连接
def kylin_connect():
    """
    连接后端项目的数据库
    aim：保护数据库信息
    :return: 连接上的数据库
    """
    db = pymysql.connect(host='172.17.66.19', user='root', password='123456', database='customer_service_platform')
    return db

def add_email(driver,email_num):
    """
    添加邮箱
    :param driver:  webdriver
    :param email_num:  待添加的邮箱
    """
    time.sleep(2)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[2]/section/div[1]/div[1]/div/form/div[7]/div/button/span"
                        ).click()
    email_input = driver.find_element(By.XPATH,
                                      "/html/body/div[1]/div/div[2]/section/div[1]/div[2]/div/div[2]/form/div/div/div/input"
                                      )
    email_input.send_keys(email_num)
    time.sleep(2)
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/div/div[2]/section/div[1]/div[2]/div/div[3]/div/button[2]/span"
                        ).click()

def add_product(driver):
    # add product
    """
    添加产品
    :param driver:  webdriver
    """
    time.sleep(1)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[2]/section/div[1]/div/div/div[2]/form/div[4]/div/button/span"
                        ).click()
    time.sleep(1)
    top_input = driver.find_elements(By.CLASS_NAME, "el-input__inner")
    time_now = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())
    top_input[0].send_keys("c" + time_now)
    top_input[1].send_keys("v1.0")

    time.sleep(1)
    # follow_button = driver.find_elements(By.CLASS_NAME, "el-button")
    # add picture
    time.sleep(1)
    follow_button0 = driver.find_element(By.CSS_SELECTOR,
                                         '.el-upload--picture > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button0.click()
    pic_location = get_upload_directory() + '\\' + "green.jpg"
    upload_file(driver,pic_location)

    # add product selector
    time.sleep(3)
    follow_button1 = driver.find_element(By.CSS_SELECTOR,
                                         'div.el-form-item:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button1 .click()
    selector_location = get_upload_directory() + '\\' + 'product_selector.csv'
    upload_file(driver,selector_location)
    time.sleep(1)

    # add module
    time.sleep(3)
    # driver.execute_script("arguments[0].scrollIntoView();",follow_button[2])
    follow_button2 = driver.find_element(By.CSS_SELECTOR,
                                         'div.el-form-item:nth-child(6) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button2.click()
    module_location = get_upload_directory() + '\\' + "csvtest2.csv"
    INFO(module_location)
    upload_file(driver, module_location)
    time.sleep(1)
    # add email
    time.sleep(2)
    follow_button3 = driver.find_element(By.CSS_SELECTOR,
                                         'div.el-form-item:nth-child(7) > div:nth-child(2) > button:nth-child(1) > span:nth-child(1)'
                                          )
    follow_button3.click()
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/div/div[2]/section/div[1]/div[2]/div/div[2]/form/div/div/div/input"
                        ).send_keys("sunzibingfashan@163.com")
    # quxiao
    time.sleep(1)
    driver.find_element(By.XPATH,
                    "/html/body/div[1]/div/div[2]/section/div[1]/div[2]/div/div[3]/div/button[1]/span"
                        ).click()
    add_email(driver,"sunzibingfashan@163.com")
    # 编辑
    time.sleep(1)
    driver.find_element(By.XPATH,
                    "/html/body/div/div/div[2]/section/div[1]/div[1]/div/form/div[7]/div/div/button[1]/span"
                        ).click()
    email_input = driver.find_element(By.XPATH,
                                      "/html/body/div[1]/div/div[2]/section/div[1]/div[2]/div/div[2]/form/div/div/div/input"
                                      )
    email_input.clear()
    email_input.send_keys("sunzibingfashan@126.com")
    time.sleep(2)
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/div/div[2]/section/div[1]/div[2]/div/div[3]/div/button[2]/span"
                        ).click()
    # delete email
    time.sleep(1)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[2]/section/div[1]/div[1]/div/form/div[7]/div/div/button[2]/span"
                        ).click()

    # add descript
    driver.find_element(By.CLASS_NAME, "el-textarea__inner"
                        ).send_keys("some")
    # reset
    time.sleep(1)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[2]/section/div[1]/div[1]/div/form/div[10]/div/button[2]/span"
                        ).click()
    # then input again
    time.sleep(1)
    top_input = driver.find_elements(By.CLASS_NAME, "el-input__inner")
    time_now = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())
    top_input[0].send_keys("c" + time_now)
    top_input[1].send_keys("v1.0")

    time.sleep(1)
    # add picture
    time.sleep(1)
    follow_button0 = driver.find_element(By.CSS_SELECTOR,
                                         '.el-upload--picture > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button0.click()
    pic_location = get_upload_directory() + '\\' + "green.jpg"
    upload_file(driver,pic_location)

    # add product selector
    time.sleep(3)
    follow_button1 = driver.find_element(By.CSS_SELECTOR,
                                         'div.el-form-item:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button1.click()
    selector_location = get_upload_directory() + '\\' + 'product_selector.csv'
    upload_file(driver, selector_location)
    time.sleep(1)

    # add module
    time.sleep(3)
    # driver.execute_script("arguments[0].scrollIntoView();",follow_button[2])
    follow_button2 = driver.find_element(By.CSS_SELECTOR,
                                         'div.el-form-item:nth-child(6) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button2.click()
    module_location = get_upload_directory() + '\\' + "csvtest2.csv"
    print(module_location)
    upload_file(driver,module_location)
    time.sleep(1)
    # add email
    add_email(driver,"sunzibingfashan@163.com")

    # 2 add accessory
    time.sleep(1)
    follow_button4 = driver.find_element(By.CSS_SELECTOR,
                                         'div.el-form-item:nth-child(8) > div:nth-child(2) > button:nth-child(1) > span:nth-child(1)'
                                         )
    follow_button4.click()
    driver.find_element(By.CLASS_NAME, "el-input__suffix").click()
    time.sleep(1)
    checkbox = driver.find_elements(By.CLASS_NAME, "el-checkbox__inner")
    # can change into fun
    time.sleep(1)
    checkbox[1].click()
    time.sleep(1)
    ActionChains(driver).move_by_offset(200, 0).click().perform()
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, "uploader-btn").click()
    iso_location = get_upload_directory() + '\\' + "csvtest1.iso"
    upload_file(driver,iso_location,0)
    time.sleep(2)
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/div/div[2]/section/div[1]/div[3]/div/div[3]/div/button[2]/span"
                        ).click()

    # add descript
    driver.find_element(By.CLASS_NAME, "el-textarea__inner"
                        ).send_keys("some")
    # commit
    time.sleep(1)
    driver.find_element(By.XPATH,
                        "/html/body/div/div/div[2]/section/div[1]/div[1]/div/form/div[10]/div/button[1]/span"
                        ).click()
    time.sleep(5)

def application_two_fill_in(driver):
    """
    填写申请表二
    :param driver:  webdriver
    """
    form_items = driver.find_elements_by_class_name("el-form-item")

    INFO("请选择架构（多选项、必填）")
    architecture = form_items[0].find_element_by_class_name("el-input__inner")
    INFO(1)

    architecture.click()
    time.sleep(1)
    architecture_list = driver.find_element_by_class_name("el-cascader-panel")

    checkbox = architecture_list.find_element_by_class_name("el-checkbox")
    checkbox.click()
    ActionChains(driver).move_by_offset(300, 0).click().perform()
    time.sleep(1)
    # content_application_two.append('FT1500A 、FT2000/4 、FT2000+ 、KunPeng920 、KunPeng990')
    # 获取选择项
    form_items = driver.find_elements_by_class_name("el-form-item")
    INFO(len(form_items))

    selected_architectures_level_zero = form_items[0].find_elements_by_class_name("el-tag")
    selected_architectures_level_one = []

    for item in selected_architectures_level_zero:
        selected_architectures_level_one.append(item.find_element_by_tag_name("span").text.split(" / ")[2])
    INFO(selected_architectures_level_one)
    selected_architectures_string = ' 、'.join(selected_architectures_level_one)


    INFO("请填写服务器主机适配的CPU型号（多选项、必填）\n请填写服务器适配的操作系统（多选项、必填）\n请填写终端主机适配的CPU型号（多选项、必填）\n请填写终端主机适配的操作系统（多选项、必填）")
    for i in [1, 2, 3, 4]:
        checkboxs = form_items[i].find_elements_by_class_name("el-checkbox__input")
        items = form_items[i].find_elements_by_class_name("el-checkbox__label")
        selected_index = random.randint(0, len(checkboxs) - 2)
        INFO("选择的一项为" + str(selected_index))
        checkboxs[selected_index].click()
        # blank_click(driver)
        INFO(items[selected_index].text)


    INFO("是否需要通过802.1x认证\n是否需要终端状态监控功能\n是否需要批量激活功能\n是否有软件统一下发的需求")
    index = 3
    for i in [5, 6, 7, 8, 10]:
        dropdown_input = form_items[i].find_element_by_class_name("el-input__inner")
        dropdown_input.click()
        time.sleep(1)
        # 下拉选项框定位
        dropdown = driver.find_element_by_xpath(f"/html/body/div[{index}]/div[1]/div[1]/ul")
        index += 1
        dropdown_selections = dropdown.find_elements_by_tag_name("li")
        dropdown_selections[0].click()
        ActionChains(driver).move_by_offset(50, 0).click().perform()
        ActionChains(driver).move_by_offset(-50, 0).perform()


    INFO("请填写预计最大管控多少台终端")
    control_count = form_items[9].find_element_by_class_name("el-input__inner")
    control_count.send_keys('wfggqqgqqq1')
    time.sleep(1)
    # blank_click(driver)


    INFO("是否还有其他需求")
    other_need = form_items[-1].find_element_by_class_name("el-input__inner")
    other_need.send_keys('input_ster')
    time.sleep(1)
    # blank_click(driver)


    INFO("点击提交按钮")
    operations = driver.find_element_by_class_name("step-button").find_elements_by_tag_name("button")
    operations[1].click()

def check_detail(driver):
    """
        点击查看详情
        :param driver:  webdriver
        return：返回详情里的第一行文字
        """
    time.sleep(1)
    driver.find_element(By.XPATH,
                      "/html/body/div/div/div[2]/section/div[1]/div/ul/li[1]/div/div/div[2]/button[3]/span"
                      ).click()
    time.sleep(1)
    temp_first_word = driver.find_element(By.XPATH,
                                        "/html/body/div[1]/div/div[2]/section/div[1]/div/div[2]/div/div[2]/div[1]/span[1]"
                                        ).text
    # close may
    time.sleep(1)
    driver.find_element(By.XPATH,
                      "/html/body/div[1]/div/div[2]/section/div[1]/div/div[2]/div/div[3]/span/button/span"
                      ).click()
    return temp_first_word

def check_detail_apply(driver):
    """
        点击查看申请详情
        :param driver:  webdriver
        return：返回详情里的第一行文字
        """
    time.sleep(1)
    driver.find_element(By.XPATH,
    "/html/body/div/div/div[2]/section/div[1]/div/div/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[10]/div/button[3]/span"
                       ).click()
    time.sleep(1)
    temp_first_word = driver.find_element(By.XPATH,
                            "/html/body/div[1]/div/div[2]/section/div[1]/div/div/div[4]/div/div[1]/span"
                                          ).text
    # close may
    time.sleep(1)
    driver.find_element(By.XPATH,
    "/html/body/div[1]/div/div[2]/section/div[1]/div/div/div[4]/div/div[3]/div/button/span"
                       ).click()
    return temp_first_word

def commit_apply(driver):
    """
    提交申请
    :param driver:  webdriver
    """
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR,
                           ".el-card__body > ul:nth-child(2) > li:nth-child(4) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > button:nth-child(2) > span:nth-child(1)"
                           ).click()
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "12312341234")
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(2) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "xiangmu")
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(3) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "danwei")
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(4) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "tom")
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(5) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "12312341234")
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(6) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "tom")
    driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(7) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "12341234123")
    driver.find_element(By.CSS_SELECTOR,
                      ".el-button--small > span:nth-child(1)").click()
    upload_file1 = get_upload_directory() + '\\' + 'mcode.csv'
    upload_file(driver,upload_file1)

    time.sleep(3)
    driver.find_element(By.CSS_SELECTOR, ".next-step").click()
    time.sleep(2)

    application_two_fill_in(driver=driver)
    time.sleep(15)


def commit_apply_productor(driver):
    """
        产品管理员提交申请
        :param driver:  webdriver
        """
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "12312341234")
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(2) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "xiangmu")
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(3) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "danwei")
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(4) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "tom")
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(5) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "12312341234")
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(6) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "tom")
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(7) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
        "12341234123")
    driver.find_element(By.CSS_SELECTOR,
                        ".el-button--small > span:nth-child(1)").click()
    upload_file1 = get_upload_directory() + '\\' + 'mcode.csv'
    upload_file(driver, upload_file1)

    time.sleep(3)
    driver.find_element(By.CSS_SELECTOR, ".next-step").click()
    time.sleep(2)

    application_two_fill_in(driver=driver)
    time.sleep(15)


def check_file():
    """
    检查文件夹中文件数量，删除下载的文件
    return：检查时的文件数量与名称
    """
    file_path = get_download_directory()
    file_num = len(os.listdir(file_path))
    file_status_list = []
    INFO("现在的文件数量")
    INFO(file_num)
    file_status_list.append(file_num)
    # 删除下载的文件
    file_name = file_path + "\\" + os.listdir(file_path)[0]
    os.remove(file_name)
    INFO(file_name)
    file_status_list.append(file_name)
    return file_status_list

def download_module(driver):
    # 下载模板
  time.sleep(2)
  driver.find_element(By.CSS_SELECTOR,
                           ".el-card__body > ul:nth-child(2) > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > button:nth-child(1) > span:nth-child(1)"
                      ).click()

def edit_product(driver):
    """
    产品管理员与超级管理员编辑产品
    :param driver:  webdriver
    return：返回编辑后的产品描述
    """
    # add picture
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR,
                        ".el-upload--picture > button:nth-child(1) > span:nth-child(1)"
                        ).click()
    pic_location = get_upload_directory() + '\\' + "stars.jpg"
    upload_file_enter(driver,By.CLASS_NAME,'el-upload-list__item-thumbnail',pic_location)

    # add product selector
    time.sleep(3)
    driver.find_element(By.CSS_SELECTOR,
        ".el-dialog__body > form:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
    ).click()
    selector_location = get_upload_directory() + '\\' + 'product_selector.csv'
    upload_file_enter(driver,By.CSS_SELECTOR,
                      'div.el-dialog__wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(2) > form:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(3) > li:nth-child(1) > a:nth-child(1)',
                      selector_location)
    time.sleep(1)

    # add module
    time.sleep(3)
    driver.find_element(By.CSS_SELECTOR,
        ".el-dialog__body > form:nth-child(1) > div:nth-child(4) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
    ).click()
    module_location = get_upload_directory() + '\\' + "csvtest2.csv"
    upload_file_enter(driver,By.CSS_SELECTOR,
                      'div.el-dialog__wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(2) > form:nth-child(1) > div:nth-child(4) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(3) > li:nth-child(1) > a:nth-child(1)',
                      module_location)
    time.sleep(1)

    # 修改附件 may more
    STEP(3,"修改附件")
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-form-item:nth-child(6) > div:nth-child(2) > div:nth-child(1) > button:nth-child(3) > span:nth-child(1)"
                        ).click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-dialog__wrapper:nth-child(5) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > button:nth-child(2)"
                        ).click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR,
            "button.el-button:nth-child(4) > span:nth-child(1)"
    ).click()
    STEP(4,"add accessory")
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR,
                        'div.el-form-item:nth-child(6) > div:nth-child(2) > button:nth-child(1) > span:nth-child(1)'
                         ).click()
    driver.find_element(By.CLASS_NAME, "el-input__suffix").click()
    time.sleep(1)
    checkbox = driver.find_elements(By.CLASS_NAME, "el-checkbox__inner")
    # can change into fun
    time.sleep(1)
    checkbox[0].click()
    time.sleep(1)
    ActionChains(driver).move_by_offset(200, 0).click().perform()
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, "uploader-btn").click()
    iso_location = get_upload_directory() + '\\' + "csvtest1.iso"
    upload_file_enter(driver,By.CLASS_NAME,'uploader-file-info',iso_location)
    # pyautogui.typewrite(iso_location)
    # time.sleep(1)
    # # 需要多按几下
    # while 1:
    #     pyautogui.press("enter", presses=2)
    #     # pyautogui.press("enter", presses=6)
    #     try:
    #         driver.find_element(By.CSS_SELECTOR,
    #                             'div.el-dialog__wrapper:nth-child(5) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)'
    #                             )
    #         break
    #     except NoSuchElementException:
    #         print("回车没有按够")
    #         continue
    time.sleep(1)
    driver.find_element(By.XPATH,
                        "/html/body/div[1]/div/div[2]/section/div[1]/div/div/div[5]/div/div[3]/div/button[2]/span"
                        ).click()

    # add descript
    descript = driver.find_element(By.CLASS_NAME, "el-textarea__inner"
                        )
    descript.clear()
    random_num = random.randint(1,19)
    edit_words = "some" + str(random_num)
    descript.send_keys(edit_words)

    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR,
                        "div.el-dialog__wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > button:nth-child(2) > span:nth-child(1)"
                         ).click()
    # wait = WebDriverWait(driver, 10, 0.5)
    # wait.until(lambda diver: driver.find_element(By.CSS_SELECTOR,
    #                                           '.avatar-wrapper > span:nth-child(1)'
    #                                           ))
    time.sleep(6)
    return edit_words

def get_global_webdriver():
    return GSTORE['global_webdriver']

# add from c73248d1&2

def get_colum(driver,colum_num):
    """
    获取某列中第一个内容
    :param driver:  webdriver
    :param colum_num：列数，第几列
    return：返回详情里的第一行文字
    """
    table_loc = driver.find_element(By.CLASS_NAME,
                "el-table__body"
                                 )
    # 按行查询表格的数据，取出的数据是一整行，按空格分隔每一列的数据
    table_tr_list = table_loc.find_elements(By.TAG_NAME, 'tr')
    # table_row_length = len(table_tr_list)
    # INFO(table_tr_list) 为引用
    # name_list = []
    # for i in table_tr_list[1:]:
    tmp = table_tr_list[0].find_elements(By.CLASS_NAME,"cell")[colum_num].text
        # name_list.append(tmp)
    # INFO(name_list)
    # ['NAME--25', 'NAME--25', 'NAME--28',
    return tmp

def get_colum_list(driver,colum_num):
    """
    获取某列中所有内容
    :param driver:  webdriver
    :param colum_num：列数，第几列
    return：返回详情里的文字列表
    """
    table_loc = driver.find_element(By.XPATH,
                                    "/html/body/div/div/div[2]/section/div[1]/div/div/div[2]/div[1]"
                                    )
    # 按行查询表格的数据，取出的数据是一整行，按空格分隔每一列的数据
    table_tr_list = table_loc.find_elements(By.TAG_NAME, 'tr')
    # table_row_length = len(table_tr_list)
    # INFO(table_tr_list) 为引用
    name_list = []
    for i in table_tr_list[1:]:
        tmp = i.find_elements_by_class_name("cell")[colum_num].text
        name_list.append(tmp)
    # INFO(name_list)
    # ['NAME--25', 'NAME--25', 'NAME--28',
    return name_list

def isElementPresent(driver, by, value):
    """
    判断元素是否存在
    :param driver:  webdriver
    :param by:  元素查找方式
    :param value: 元素查找值
    :return: 元素是否存在
    """
    try:
        driver.find_element(by=by, value=value)
        return True
    except NoSuchElementException:
        return False

def message_check(driver,css_s):
    """
    信息检查
    :param driver:  webdriver
    :param css_s：css选择器内容，需要定位的位置
    return：返回取到弹窗里的内容
    """
    getted1 = False
    error = ""
    # a = self.driver.find_element(By.CSS_SELECTOR, "body > div.el-message.el-message--error > p").text
    while not getted1:
        try:
            error += driver.find_element(By.CSS_SELECTOR,css_s
                                             ).text
        except Exception:
            getted1 = False
            print(False)
            continue
        getted1 = True
        return error

def open_browser():
    # 打开浏览器，并且设置下载位置和直接下载
    profile = webdriver.FirefoxProfile()
    download_location = get_download_directory()
    profile.set_preference('browser.download.dir', download_location)
    profile.set_preference('browser.download.folderList', 2)
    profile.set_preference('browser.download.manager.showWhenStarting', False)
    profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream')

    wd = webdriver.Firefox(firefox_profile=profile)
    INFO('打开浏览器')

    wd.implicitly_wait(5)

    # 使用黑羽robot 全局存储对象 GSTORE
    GSTORE['global_webdriver'] = wd
    return wd

def productor_login(wd):
    """
    产品管理员登录
    :param wd:  webdriver
    """
    wd.get("http://172.17.66.19:8080/")
    productor_username = get_username(1)
    productor_password = get_password(1)
    wd.find_element(By.NAME, "username").send_keys(productor_username)
    wd.find_element(By.NAME, "password").send_keys(productor_password)
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def productor1_login(wd):
    """
    产品管理员1登录
    :param wd:  webdriver
    """
    wd.get("http://172.17.66.19:8080/")
    productor1_username = get_username(2)
    productor1_password = get_password(2)
    wd.find_element(By.NAME, "username").send_keys(productor1_username)
    wd.find_element(By.NAME, "password").send_keys(productor1_password)
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def productor2_login(wd):
    """
    产品管理员2登录
    :param wd:  webdriver
    """
    wd.get("http://172.17.66.19:8080/")
    productor1_username = get_username(3)
    productor1_password = get_password(3)
    wd.find_element(By.NAME, "username").send_keys(productor1_username)
    wd.find_element(By.NAME, "password").send_keys(productor1_password)
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def select_message_list(driver,input_project_name,input_product_name):
    """
    查询信息列表
    :param driver:  webdriver
    :param input_project_name：输入项目名称
    :param input_product_name：输入产品名称
    """
    project_input = driver.find_element(By.XPATH,
                "/html/body/div[1]/div/div[2]/section/div[1]/div/div/div[2]/form/div[1]/div/div/input"
                        )
    project_input.clear()
    project_input.send_keys(input_project_name)
    product_input =driver.find_element(By.XPATH,
            "/html/body/div[1]/div/div[2]/section/div[1]/div/div/div[2]/form/div[2]/div/div/input"
                        )
    product_input.clear()
    product_input.send_keys(input_product_name)
    # click select
    time.sleep(1)
    driver.find_element(By.XPATH,
    "/html/body/div[1]/div/div[2]/section/div[1]/div/div/div[2]/form/div[3]/div/button/span"
    ).click()

def super_login(wd):
    """
    超级管理员登录
    :param wd:  webdriver
    """
    wd.get("http://172.17.66.19:8080/")
    super_username = get_username(4)
    super_password = get_password(4)
    wd.find_element(By.NAME, "username").send_keys(super_username)
    wd.find_element(By.NAME, "password").send_keys(super_password)
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def turn_page(driver):
    """
    翻页
    :param driver:  webdriver
    """
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".el-pagination__editor > input:nth-child(1)").clear()
    driver.find_element(By.CSS_SELECTOR, ".el-pagination__editor > input:nth-child(1)").send_keys('3')
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, "li.number:nth-child(2)").click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".el-icon-arrow-right").click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".el-icon-arrow-left").click()

def upload_file_enter(driver, by, location, file_path):
    """
    定义点击上传按钮后上传文件时的enter操作，只针对上传正确文件，无法处理上传错误文件
    :param driver:
    :param by: 定位方式
    :param location: 元素定位
    :param file_path: 上传文件路径
    :return:
    """
    # 等待上传文件弹窗弹出
    time.sleep(3)
    pyautogui.write(file_path)
    pyautogui.press("enter")
    # 等待路径输入完成
    time.sleep(3)
    # 等待文件上传成功，若不成功，按enter再次打开上传弹窗，重新输入文件路径
    while not isElementPresent(driver, by, location):
        pyautogui.press("enter", presses=2)
        # time.sleep(1)
        # pyautogui.write(file_path)
        time.sleep(1)
        # pyautogui.press("enter")

def upload_file(wd, filename, type = 1):
    """
    上传文件
    :param wd: webdriver
    :param filename: 文件路径
    :return:
    """
    path = filename.split("\\")
    name = path[-1]
    time.sleep(1)
    pyautogui.typewrite(filename)
    while True:
        pyautogui.press("enter")
        time.sleep(2)
        try:
            if type == 1:
                wd.find_element_by_xpath('//a[@class="el-upload-list__item-name" and contains(text(),"' + name + '")]')
            else:
                wd.find_element_by_xpath('//div[@class="uploader-file-name" and contains(text(),"' + name + '")]')
            break
        except NoSuchElementException:
            try:
                wd.find_element_by_xpath('//div[@role="alert"]/p[@class="el-message__content"]')
                break
            except NoSuchElementException:
                pyautogui.typewrite(filename)
                pyautogui.press("enter")
                continue
    time.sleep(1)

def user_login(wd):
    """
    普通用户登录
    :param wd:  webdriver
    """
    wd.get("http://172.17.66.19:8080/")
    user_username = get_username(0)
    user_password = get_password(0)
    wd.find_element(By.NAME, "username").send_keys(user_username)
    wd.find_element(By.NAME, "password").send_keys(user_password)
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

# 获取 全局使用的 webdriver 对象











# def upload_file(driver, location):
#     # time.sleep(3)
#     pyautogui.write(location)
#     # time.sleep(3)
#     # pyautogui.press("enter")
#     time.sleep(5)
#     # 需要多按几下
#     while 1:
#         pyautogui.press("enter", presses=2)
#         time.sleep(1)
#         # pyautogui.press("enter", presses=6)
#         try:
#             driver.find_element(By.CLASS_NAME, "card-title")
#             break
#         except NoSuchElementException:
#             print("回车没有按够")
#             continue
#